//一个鼠标可控的小方块
import * as THREE from "../node_modules/three/build/three.module.js";
import { OrbitControls } from "../node_modules/three/examples/jsm/controls/OrbitControls.js";

var scene, camera, renderer;
var cube;
var controls;

function init() {
  //初始化
  scene = new THREE.Scene();
  //   scene.background = new THREE.Color(0x2c3e50);

  //相机
  camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    100
  );
  // camera = new THREE.OrthographicCamera(
  //   -window.innerWidth / 100,
  //   window.innerWidth / 100,
  //   window.innerHeight / 100,
  //   -window.innerHeight / 100,
  //   0.1,
  //   100
  // );

  camera.position.set(0, 0, 20);
  camera.lookAt(new THREE.Vector3(0, 0, 0));
  scene.add(camera);

  //渲染器
  renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0x2d3436);
  document.body.appendChild(renderer.domElement);

  //点光源
  var pointLight = new THREE.PointLight(0xffffff);
  pointLight.position.set(5, 5, 0);
  scene.add(pointLight);

  //测试用的小方块
  var geometry = new THREE.BoxGeometry(10, 10, 10);
  // var material = new THREE.MeshLambertMaterial({
  //   color: 0x3498db,
  //   // emissive: 0x3498db,
  //   // wireframe: true,
  // });
  var material = new THREE.MeshNormalMaterial();

  cube = new THREE.Mesh(geometry, material);
  scene.add(cube);

  // // //纹理,平面几何体
  // var texture = new THREE.TextureLoader().load("src/img/Claire.jpg");
  // texture.wrapS = THREE.RepeatWrapping;
  // texture.wrapT = THREE.RepeatWrapping;
  // texture.repeat.set(1, 1);
  // //用贴图->材质
  // var pic = new THREE.MeshBasicMaterial({
  //   map: texture,
  // });
  // var planeGeo = new THREE.PlaneBufferGeometry(40, 40);
  // var plane = new THREE.Mesh(planeGeo, pic);
  // scene.add(plane);

  //画线
  // var lineMat = new THREE.LineBasicMaterial({ color: 0xe74c3c });
  // var points = [];
  // points.push(new THREE.Vector3(-10, 0, 0));
  // points.push(new THREE.Vector3(0, 10, 0));
  // points.push(new THREE.Vector3(10, 0, 0));
  // var lineGeo = new THREE.BufferGeometry().setFromPoints(points);
  // var line = new THREE.Line(lineGeo, lineMat);
  // scene.add(line);

  //鼠标控制器
  controls = new OrbitControls(camera, renderer.domElement);
  controls.update();
}

//动画
function animate() {
  requestAnimationFrame(animate);
  //   cube.rotation.x += 0.01;
  //   cube.rotation.y += 0.01;
  controls.update();
  renderer.render(scene, camera);
}

init();
animate();
